package com.zxy.supplier_system.server.controller

import com.zxy.supplier_system.server.service.MarketShoppingGuideSalaryDocumentService
import com.zxy.supplier_system.server.utils.IntIdAndName
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.web.bind.annotation.*
import java.math.BigDecimal
import java.time.OffsetDateTime

@RestController
@RequestMapping("markets/{marketId}/shoppingGuides/salaryDocuments/")
class MarketShoppingGuideSalaryDocumentController(
    private val marketShoppingGuideSalaryDocumentService: MarketShoppingGuideSalaryDocumentService,
) {

    data class QueryMarketShoppingGuideSalaryDocumentRequest(
        val year: Int? = null,
        val month: Int? = null,
        val employeeIds: List<Int> = emptyList(),
    )

    @GetMapping("")
    fun queryMarketShoppingGuideSalaryDocuments(
        @PathVariable marketId: Int,
        request: QueryMarketShoppingGuideSalaryDocumentRequest,
    ): Page<QueryMarketShoppingGuideSalaryDocumentResponse> {
        return this.marketShoppingGuideSalaryDocumentService.queryMarketShoppingGuideSalaryDocuments(marketId, request)
    }

    data class QueryMarketShoppingGuideSalaryDocumentResponse(
        val id: Int,
        val year: Int,
        val month: Int,
        val auditedDateTime: OffsetDateTime?,
        val confirmedDateTime: OffsetDateTime?,
        val paidDateTime: OffsetDateTime?,
        val note: String?,
    )

    @GetMapping("monthlySalarySummary")
    fun findMonthlySalarySummary(
        @PathVariable marketId: Int,
        pageable: Pageable
    ): Page<QueryMarketShoppingGuideSalaryDocumentGroupByMonthResponse> {
        return this.marketShoppingGuideSalaryDocumentService.findMonthlySalarySummary(marketId, pageable)
    }

    @GetMapping("previewMonthlySalaryDocuments")
    fun previewMonthlySalaryDocuments(
        @PathVariable marketId: Int,
        year: Int,
        month: Int
    ): PreviewMonthlySalaryDocumentsResponse {
        return this.marketShoppingGuideSalaryDocumentService.previewMonthlySalaryDocuments(marketId, year, month)
    }

    data class PreviewMonthlySalaryDocumentsResponse(
        val employeeSalaries: List<EmployeeSalary>,
        /**
         * 总工资
         */
        val totalSalary: BigDecimal,
        /**
         * 总销量
         */
        val totalSale: BigDecimal,
        val ruleSalaries: List<RuleSalary>
    ) {

        data class RuleSalary(
            val id:Int,
            val name: String,
            val note: String?,
            /**
             * 工资计算结果
             */
            val salary: BigDecimal,
        )

        data class EmployeeSalary(
            val id: Int,
            val name: String,
            val totalSalary: BigDecimal,
            val items: List<Item>,
        ) {
            data class Item(
                val salary: BigDecimal,
                val note: String?,
                val name: String,
            )
        }
    }


    @PostMapping("generateMonthlySalaryDocuments")
    fun generateMarketShoppingGuideSalaryDocuments(@PathVariable marketId: Int, year: Int, month: Int) {
        return this.marketShoppingGuideSalaryDocumentService.generateMarketShoppingGuideSalaryDocuments(
            marketId,
            year,
            month
        )
    }

    @GetMapping("monthlySalaryGroupByEmployee")
    fun findMonthlySalaryGroupByEmployee(@PathVariable marketId: Int, year: Int, month: Int):List<MonthlySalaryGroupByEmployeeResponse> {
        return this.marketShoppingGuideSalaryDocumentService.findMonthlySalaryGroupByEmployee(marketId, year, month)
    }

    data class MonthlySalaryGroupByEmployeeResponse(
        val id: Int,
        val employee: IntIdAndName,
        val items: List<Item>,
        val note: String?,
    ) {
        data class Item(
            val id: Int,
            val salary: BigDecimal,
            val name: String,
            val note: String?
        )
    }


}

data class QueryMarketShoppingGuideSalaryDocumentGroupByMonthResponse(
    val year: Int,
    val month: Int,
    val totalSalary: BigDecimal,
)